package com.gxd.service.impl;

import com.gxd.dao.GoodsDao;
import com.gxd.dao.impl.GoodsDaoImpl;
import com.gxd.domain.Goods;
import com.gxd.domain.GoodsType;
import com.gxd.domain.PageBean;
import com.gxd.service.GoodsService;
import com.gxd.service.GoodsTypeService;

import java.util.List;

public class GoodsServiceImpl implements GoodsService {
    GoodsDao goodsDao = new GoodsDaoImpl();
    @Override
    public PageBean findById(int typeId, int page_num, int page_size) {
        PageBean pageBean = new PageBean();
        pageBean.setPageNum(page_num);
        pageBean.setPageSize(page_size);
        long count =goodsDao.selectCount(typeId);
        pageBean.setTotalCount(count);
        List<Goods> goods=goodsDao.selectData(typeId,(page_num-1)*page_size,page_size);
        pageBean.setData(goods);


        return pageBean;
    }

    @Override
    public Goods findGoodById(int id) {
        Goods goods = goodsDao.findGoodById(id);
        GoodsTypeService goodsTypeService = new GoodsTypeServiceImpl();
        GoodsType goodsType=goodsTypeService.findById(goods.getTypeid());
        goods.setGoodsType(goodsType);
        return goods;
    }

    @Override
    public PageBean findByPage(int page_num, int page_size) {
        long totalCount = goodsDao.selectCount();
        List<Goods> data=goodsDao.selectByPage(page_num,page_size);
        PageBean pageBean= new PageBean();
        pageBean.setPageNum(page_num);
        pageBean.setPageSize(page_size);
        pageBean.setData(data);
        pageBean.setTotalCount(totalCount);
        return pageBean;
    }

    @Override
    public void addGoods(Goods goods) {
        goodsDao.addGoods(goods);
    }
}
